<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #tableTitle>
        <ButtonGroup>
          <a-button type="primary" @click="handleCreate()" v-if="checkAuth('ADD-DEPT')">
            + 新增部门
          </a-button>
          <BasicImport
            @success="handleImport"
            :api="uploadDeptApi"
            :tempApi="tempDeptApi"
            v-if="checkAuth('TO-CHANNEL')"
          >
            <a-button> 导入 </a-button>
          </BasicImport>
          <a-button @click="handleExport" v-if="checkAuth('EXPORT')"> 导出 </a-button>
        </ButtonGroup>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                ifShow: checkAuth('TABLE-ADD'),
                // icon: 'clarity:blocks-group-solid-badged',
                label: '新增',
                onClick: handleCreate.bind(null, record),
              },
              {
                ifShow: checkAuth('TABLE-EDIT'),
                // icon: 'clarity:note-edit-line',
                label: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                ifShow: checkAuth('TABLE-DELETE'),
                // icon: 'ant-design:delete-outlined',
                label: '删除',
                // color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <DeptModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { Button } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import DeptModal from './DeptModal.vue';
  import { BasicImport } from '/@/components/ImportButton';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadByData } from '/@/utils/file/download';
  import { getBtnAuth, checkAuth } from '/@/utils/cutomPageBtnAuth/index';
  import { useRoute } from 'vue-router';
  import {
    getDeptTreeByPage,
    deleteDeptApi,
    exportDeptApi,
    tempDeptApi,
    uploadDeptApi,
  } from '/@/api/admin/dept';
  import { columns, searchFormSchema } from './dept.data';

  const ButtonGroup = Button.Group;

  export default defineComponent({
    name: 'DeptManagement',
    components: { BasicTable, DeptModal, TableAction, BasicImport, ButtonGroup },
    setup() {
      const route = useRoute();

      const { createMessage } = useMessage();

      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, expandAll }] = useTable({
        title: '部门列表',
        api: getDeptTreeByPage,
        columns,
        formConfig: {
          labelWidth: 75,
          schemas: searchFormSchema,
        },
        pagination: false,
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        immediate: false,
        isTreeTable: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
        },
        afterFetch(res) {
          // 调整空数据时表格的高度
          const mainContent = document.querySelector('.vben-layout-content') as HTMLElement;
          const table = mainContent?.querySelector('tbody') as HTMLElement;
          if (!res.length) {
            setTimeout(() => {
              table.style.height = mainContent.offsetHeight - 200 + 'px';
            }, 150);
          } else {
            table.style.height = 'auto';
          }
        },
      });
      getBtnAuth(route, reload);
      function handleCreate(record: Recordable = {}) {
        openModal(true, {
          isUpdate: false,
          record,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await deleteDeptApi(record.id);
        createMessage.success('操作成功');
        reload();
      }

      function handleSuccess() {
        reload();
      }
      function handleImport() {
        createMessage.success('操作成功');
        reload();
      }
      async function handleExport() {
        const file = await exportDeptApi();
        downloadByData(file, `dept_${Date.now()}.xlsx`);
      }
      function onFetchSuccess() {
        nextTick(expandAll);
      }
      return {
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleImport,
        handleExport,
        tempDeptApi,
        uploadDeptApi,
        onFetchSuccess,
        checkAuth,
      };
    },
  });
</script>

<style scoped lang="less">
  :deep(.vben-basic-table .ant-table-wrapper) {
    padding-left: 16px;
    padding-top: 16px;
  }

  :deep(.vben-basic-table .ant-table-wrapper .ant-table-title) {
    padding-bottom: 16px !important;
  }

  :deep(.ant-btn-group) {
    .ant-btn {
      margin-right: 16px;
      border-radius: 4px;
    }
  }
</style>
